//
//  pushNetBookController.swift
//  好书
//
//  Created by WZH on 2018/3/29.
//  Copyright © 2018年 WZH. All rights reserved.
//

import UIKit

class pushNetBookController: UIViewController,BookTitleDelegate,PhotoPickerDelegate,VPImageCropperDelegate,UITableViewDelegate,UITableViewDataSource {

    var tableView:UITableView?

    var BookTitle:BookTitleView?
    
    var titleArray:NSMutableArray?
    
    var Book_Title:String?
    
    var Score:LDXScore?
    
    var showScore = false
    
    var type = "文学"
    var detailType = "文学"
    var Book_Description = ""
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        
        self.BookTitle = BookTitleView(frame: CGRect(x: 0, y: 40, width: SCREEN_WIDTH, height: 160))
        self.BookTitle?.delegate = self
        self.view.addSubview(self.BookTitle!)
        
        self.tableView = UITableView(frame: CGRect(x: 0, y: 200, width: SCREEN_WIDTH, height: SCREEN_HEIGHT - 200), style: .grouped)
        self.tableView?.tableFooterView = UIView()
        self.tableView?.delegate = self
        self.tableView?.dataSource = self
        self.tableView?.register(UITableViewCell.classForCoder(), forCellReuseIdentifier: "cell")
        self.tableView?.backgroundColor = UIColor(red: 250/255, green: 250/255, blue: 250/255, alpha: 1)
        self.view.addSubview(self.tableView!)
        self.titleArray = ["标题","评分","分类","书评"]
        
        self.Score = LDXScore(frame: CGRect(x: 100, y: 10, width: 100, height: 22))
        self.Score?.isSelect = true
        self.Score?.normalImg = UIImage(named: "btn_star_evaluation_normal")
        self.Score?.highlightImg = UIImage(named: "btn_star_evaluation_press")
        self.Score?.max_star = 5
        self.Score?.show_star = 5
        
    }
    @objc func close () {
        self.dismiss(animated: true) {
            
        }
    }
    
    @objc func sure () {
        self.dismiss(animated: true) {
            
        }
    }
    func choiceCover() {
        let vc = PhotoPickerViewController()
        vc.delegate = self
        self.present(vc, animated: true) {
            
        }
        
    }
    
    func getImageFromPicker(image: UIImage) {
        let croVC = VPImageCropperViewController(image: image, cropFrame: CGRect(x: 0, y: 100, width: SCREEN_WIDTH, height: SCREEN_WIDTH * 1.273), limitScaleRatio: 3)
        croVC?.delegate = self
        self.present(croVC!, animated: true) {
            
        }
    }
    
    func imageCropperDidCancel(_ cropperViewController: VPImageCropperViewController!) {
        cropperViewController.dismiss(animated: true) {
            
        }
    }
    
    func imageCropper(_ cropperViewController: VPImageCropperViewController!, didFinished editedImage: UIImage!) {
        self.BookTitle?.BookCover?.setImage(editedImage, for: .normal)
        cropperViewController.dismiss(animated: true) {
            
        }
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return (self.titleArray?.count)!
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .value1, reuseIdentifier: "cell")
        for view in cell.contentView.subviews {
            view.removeFromSuperview()
        }
        var row = indexPath.row
        if self.showScore && row > 1 {
            row = row - 1
        }
        
        if indexPath.row != 1 {
            cell.accessoryType = .disclosureIndicator
        }
        cell.textLabel?.text = self.titleArray?[indexPath.row] as? String
        cell.textLabel?.font = UIFont(name: MY_FONT, size: 15)
        cell.detailTextLabel?.font = UIFont(name: MY_FONT, size: 13)
        cell.selectionStyle = .none
        switch row {
        case 0:
            cell.detailTextLabel?.text = self.Book_Title
        case 2:
            cell.detailTextLabel?.text = self.type + "->" + self.detailType
        case 4:
            cell.accessoryType = .none
            let commentView = UITextView(frame: CGRect(x: 4, y: 4, width: SCREEN_WIDTH - 8, height: 80))
            commentView.text = self.Book_Description
            commentView.font = UIFont(name: MY_FONT, size: 14) 
            cell.contentView.addSubview(commentView)
        default:
            ()
        }
        if self.showScore && indexPath.row == 2 {
            cell.contentView .addSubview(self.Score!)
        }
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if self.showScore && indexPath.row > 5 {
            return 88
        }else if !self.showScore && indexPath.row > 4 {
            return 88
        }else{
            return 44
        }
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        self.tableView?.deselectRow(at: indexPath, animated: true)
        var row = indexPath.row
        if self.showScore && row > 1 {
            row -= 1
        }
        
        switch row {
        case 0:
            tableViewSelectTitle()
        case 1:
            tableViewSelectScore()
        case 2:
            tableViewSelectType()
        case 3:
            tableViewSelectDescription()
        default:
            ()
        }
    }
    
    /// 选择标题
    func tableViewSelectTitle() {
        let vc = Push_TitleController()
        GeneralFactory.addTitleWithTitle(target: vc)
        vc.callBack = ({(Title:String)->Void in
            self.Book_Title = Title
            self.tableView?.reloadData()
        })
        self.present(vc, animated: true) {
            
        }
    }
    /// 选择评分
    func tableViewSelectScore() {
        self.tableView?.beginUpdates()
        
        let temIndexPath = NSIndexPath(row: 2, section: 0)
        if self.showScore {
            self.titleArray?.removeObject(at: 2)
            self.tableView?.deleteRows(at: [temIndexPath as IndexPath], with: .middle)
            self.showScore = false
        }else {
            self.tableView?.insertRows(at: [temIndexPath as IndexPath], with: .middle)
            self.titleArray?.insert("", at: 2)
            self.showScore = true
        }
        self.tableView?.endUpdates()
    }
    /// 选择分类
    func tableViewSelectType() {
        let vc = Push_TypeViewController()
        GeneralFactory.addTitleWithTitle(target: vc)
        let btn1 = vc.view.viewWithTag(1234) as! UIButton
        let btn2 = vc.view.viewWithTag(1235) as! UIButton
        btn1.setTitleColor(RGB(r: 38, g: 82, b: 67), for: .normal)
        btn2.setTitleColor(RGB(r: 38, g: 82, b: 67), for: .normal)
        vc.detailType = self.detailType
        vc.type = self.type
        vc.callBack = ({(type:String,detailtype:String)->Void in
            self.type = type
            self.detailType = detailtype
            self.tableView?.reloadData()
        })
        
        self.present(vc, animated: true) {
            
        }
        
    }
    /// 选择书评
    func tableViewSelectDescription() {
        let vc = Push_DescriptionController()
        GeneralFactory.addTitleWithTitle(target: vc)
        vc.textView?.text = self.Book_Description
        vc.callBack = ({(description:String)-> Void in
            self.Book_Description = description
            if self.titleArray?.lastObject as? String == "" {
                self.titleArray?.removeLastObject()
            }
            if description != "" {
                self.titleArray?.add("")
            }
            self.tableView?.reloadData()
        })
        self.present(vc, animated: true) {
            
        }
        
        print("tableViewSelectDescription")
    }
    deinit {
        print("pushNetBookController reallse")
    }
    
}
